package com.applink.sell_proof.builder;

import android.content.Context;
import android.os.AsyncTask;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.applink.sell_proof.adapter.ListBaseAdapter;

public abstract class AbstractListBuilder extends AbstractBuilder {

	protected ListView mListView;

	public AbstractListBuilder(Context context, ViewGroup parent) {
		super(context, parent);

		mListView = (ListView) parent.findViewById(android.R.id.list);
		mListView.setAdapter(mAdapter);
	}

	/**
	 * another constructor incase that we need another custom ListView instead
	 * the default one with android.R.id.list
	 * 
	 * @author Van Vu
	 * @param context
	 * @param parent
	 * @param lv
	 */
	public AbstractListBuilder(Context context, ViewGroup parent, ListView lv) {
		super(context, parent);
		mListView = lv;
		mListView.setAdapter(mAdapter);
	}

	protected BaseAdapter getAdapter() {
		if (mAdapter == null) {
			// return a new BaseAdapter extended instance
			return new ListBaseAdapter(this);
		} else {
			return mAdapter;
		}
	}

	@Override
	public void buildData() {
		new BuildData().execute((Void) null);
	}

	public abstract void notifyDataChange();

	class BuildData extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			buildData();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			notifyDataChange();
		}
	}
}
